import sys
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout,
                             QHBoxLayout, QLabel, QLineEdit, QMessageBox, QStackedWidget,
                             QGroupBox, QFormLayout, QDialog, QTabWidget, QTableWidget,
                             QTableWidgetItem, QHeaderView, QComboBox, QTextEdit, QGridLayout,
                             QSpinBox, QRadioButton, QButtonGroup)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QFont

# 添加项目根目录到系统路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from database import Database
from iteration3_dormitory_allocation.allocate_dormitory import DormitoryAllocator
from iteration3_dormitory_allocation.adjust_dormitory import DormitoryAdjuster
from iteration3_dormitory_allocation.checkout_dormitory import DormitoryCheckout
from iteration3_dormitory_allocation.query_allocation import AllocationQuery

# 宿舍分配窗口
class AllocateDormitoryDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('分配宿舍')
        self.setGeometry(300, 300, 400, 200)
        self.allocator = DormitoryAllocator()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 表单布局
        form_layout = QFormLayout()
        
        self.student_id_input = QLineEdit()
        self.student_id_input.setPlaceholderText('例如: 20210001')
        form_layout.addRow('学号:', self.student_id_input)
        
        # 获取所有宿舍信息
        db = Database()
        db.connect()
        db.cursor.execute(
            "SELECT id, building, room_number FROM dormitory ORDER BY building, room_number"
        )
        dormitories = db.cursor.fetchall()
        db.close()
        
        # 宿舍选择下拉框
        self.dormitory_combo = QComboBox()
        for dorm in dormitories:
            self.dormitory_combo.addItem(f"{dorm[1]}-{dorm[2]}", dorm[0])
        form_layout.addRow('宿舍:', self.dormitory_combo)
        
        # 添加表单到主布局
        form_group = QGroupBox('分配信息')
        form_group.setLayout(form_layout)
        layout.addWidget(form_group)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.allocate_button = QPushButton('分配')
        self.allocate_button.clicked.connect(self.allocate_dormitory)
        button_layout.addWidget(self.allocate_button)
        
        self.cancel_button = QPushButton('取消')
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def allocate_dormitory(self):
        student_id = self.student_id_input.text().strip()
        dormitory_id = self.dormitory_combo.currentData()
        
        if not student_id:
            QMessageBox.warning(self, '警告', '学号不能为空')
            return
        
        try:
            result = self.allocator.allocate_dormitory(student_id, dormitory_id)
            
            if result:
                QMessageBox.information(self, '成功', f'已为学号 {student_id} 分配宿舍')
                self.accept()
        except ValueError:
            QMessageBox.warning(self, '警告', '宿舍ID必须是数字')

# 批量宿舍分配窗口
class BatchAllocateDormitoryDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('批量分配宿舍')
        self.setGeometry(300, 300, 500, 400)
        self.allocator = DormitoryAllocator()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 说明标签
        instruction_label = QLabel('每行输入一个分配信息，格式：学号,宿舍楼号-房间号')
        instruction_label.setWordWrap(True)
        layout.addWidget(instruction_label)
        
        # 文本输入区域
        self.text_input = QTextEdit()
        self.text_input.setPlaceholderText('例如:\n20210001,A-101\n20210002,B-202')
        layout.addWidget(self.text_input)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.allocate_button = QPushButton('批量分配')
        self.allocate_button.clicked.connect(self.batch_allocate_dormitory)
        button_layout.addWidget(self.allocate_button)
        
        self.cancel_button = QPushButton('取消')
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def batch_allocate_dormitory(self):
        text = self.text_input.toPlainText().strip()
        if not text:
            QMessageBox.warning(self, '警告', '请输入分配信息')
            return
        
        lines = text.split('\n')
        allocations = []
        
        # 连接数据库获取宿舍信息
        db = Database()
        db.connect()
        
        for line in lines:
            parts = [part.strip() for part in line.split(',')]
            if len(parts) != 2:
                QMessageBox.warning(self, '警告', f'格式错误: {line}\n请确保包含学号和宿舍信息(楼号-房间号)')
                db.close()
                return
            
            student_id = parts[0]
            dorm_info = parts[1].split('-')
            
            if len(dorm_info) != 2:
                QMessageBox.warning(self, '警告', f'宿舍格式错误: {parts[1]}\n正确格式为: 楼号-房间号')
                db.close()
                return
                
            building, room_number = dorm_info
            
            # 查询宿舍ID
            db.cursor.execute(
                "SELECT id FROM dormitory WHERE building = ? AND room_number = ?",
                (building, room_number)
            )
            result = db.cursor.fetchone()
            
            if not result:
                QMessageBox.warning(self, '警告', f'宿舍 {building}-{room_number} 不存在')
                db.close()
                return
                
            dormitory_id = result[0]
            allocations.append((student_id, dormitory_id))
        
        db.close()
        
        if allocations:
            success_count, total_count = self.allocator.batch_allocate_dormitory(allocations)
            QMessageBox.information(self, '成功', f'批量分配完成：成功 {success_count}/{total_count}')
            self.accept()

# 宿舍调整窗口
class AdjustDormitoryDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('调整宿舍')
        self.setGeometry(300, 300, 400, 200)
        self.adjuster = DormitoryAdjuster()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 表单布局
        form_layout = QFormLayout()
        
        self.student_id_input = QLineEdit()
        self.student_id_input.setPlaceholderText('例如: 20210001')
        form_layout.addRow('学号:', self.student_id_input)
        
        # 获取所有宿舍信息
        db = Database()
        db.connect()
        db.cursor.execute(
            "SELECT id, building, room_number FROM dormitory ORDER BY building, room_number"
        )
        dormitories = db.cursor.fetchall()
        db.close()
        
        # 宿舍选择下拉框
        self.new_dormitory_combo = QComboBox()
        for dorm in dormitories:
            self.new_dormitory_combo.addItem(f"{dorm[1]}-{dorm[2]}", dorm[0])
        form_layout.addRow('新宿舍:', self.new_dormitory_combo)
        
        # 添加表单到主布局
        form_group = QGroupBox('调整信息')
        form_group.setLayout(form_layout)
        layout.addWidget(form_group)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.adjust_button = QPushButton('调整')
        self.adjust_button.clicked.connect(self.adjust_dormitory)
        button_layout.addWidget(self.adjust_button)
        
        self.cancel_button = QPushButton('取消')
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def adjust_dormitory(self):
        student_id = self.student_id_input.text().strip()
        new_dormitory_id = self.new_dormitory_combo.currentData()
        
        if not student_id:
            QMessageBox.warning(self, '警告', '学号不能为空')
            return
        
        try:
            result = self.adjuster.adjust_dormitory(student_id, new_dormitory_id)
            
            if result:
                QMessageBox.information(self, '成功', f'已为学号 {student_id} 调整宿舍')
                self.accept()
        except ValueError:
            QMessageBox.warning(self, '警告', '新宿舍ID必须是数字')

# 退宿处理窗口
class CheckoutDormitoryDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('退宿处理')
        self.setGeometry(300, 300, 400, 200)
        self.checkout = DormitoryCheckout()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 表单布局
        form_layout = QFormLayout()
        
        self.student_id_input = QLineEdit()
        self.student_id_input.setPlaceholderText('例如: 20210001')
        form_layout.addRow('学号:', self.student_id_input)
        
        # 添加表单到主布局
        form_group = QGroupBox('退宿信息')
        form_group.setLayout(form_layout)
        layout.addWidget(form_group)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.checkout_button = QPushButton('退宿')
        self.checkout_button.clicked.connect(self.checkout_dormitory)
        button_layout.addWidget(self.checkout_button)
        
        self.cancel_button = QPushButton('取消')
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def checkout_dormitory(self):
        student_id = self.student_id_input.text().strip()
        
        if not student_id:
            QMessageBox.warning(self, '警告', '学号不能为空')
            return
        
        result = self.checkout.checkout_dormitory(student_id)
        
        if result:
            QMessageBox.information(self, '成功', f'学号 {student_id} 的学生已退宿')
            self.accept()

# 批量退宿处理窗口
class BatchCheckoutDormitoryDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('批量退宿处理')
        self.setGeometry(300, 300, 500, 400)
        self.checkout = DormitoryCheckout()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 说明标签
        instruction_label = QLabel('每行输入一个学生学号')
        instruction_label.setWordWrap(True)
        layout.addWidget(instruction_label)
        
        # 文本输入区域
        self.text_input = QTextEdit()
        self.text_input.setPlaceholderText('例如:\n20210001\n20210002')
        layout.addWidget(self.text_input)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        self.checkout_button = QPushButton('批量退宿')
        self.checkout_button.clicked.connect(self.batch_checkout_dormitory)
        button_layout.addWidget(self.checkout_button)
        
        self.cancel_button = QPushButton('取消')
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def batch_checkout_dormitory(self):
        text = self.text_input.toPlainText().strip()
        if not text:
            QMessageBox.warning(self, '警告', '请输入学号')
            return
        
        student_ids = [line.strip() for line in text.split('\n') if line.strip()]
        
        if student_ids:
            success_count, total_count = self.checkout.batch_checkout_dormitory(student_ids)
            QMessageBox.information(self, '成功', f'批量退宿完成：成功 {success_count}/{total_count}')
            self.accept()

# 分配查询窗口
class QueryAllocationWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.query = AllocationQuery()
        self.initUI()
        
    def initUI(self):
        layout = QVBoxLayout()
        
        # 查询选项
        option_group = QGroupBox('查询选项')
        option_layout = QVBoxLayout()
        
        self.query_all_btn = QRadioButton('查询所有分配情况')
        self.query_all_btn.setChecked(True)
        option_layout.addWidget(self.query_all_btn)
        
        self.query_by_student_btn = QRadioButton('根据学生查询分配情况')
        option_layout.addWidget(self.query_by_student_btn)
        
        self.query_by_dormitory_btn = QRadioButton('根据宿舍查询分配情况')
        option_layout.addWidget(self.query_by_dormitory_btn)
        
        # 创建按钮组
        self.query_button_group = QButtonGroup()
        self.query_button_group.addButton(self.query_all_btn, 1)
        self.query_button_group.addButton(self.query_by_student_btn, 2)
        self.query_button_group.addButton(self.query_by_dormitory_btn, 3)
        
        option_group.setLayout(option_layout)
        layout.addWidget(option_group)
        
        # 查询参数表单
        self.param_group = QGroupBox('查询参数')
        self.param_layout = QFormLayout()
        
        self.student_id_input = QLineEdit()
        self.student_id_input.setPlaceholderText('例如: 20210001')
        self.param_layout.addRow('学号:', self.student_id_input)
        
        # 获取所有宿舍信息
        db = Database()
        db.connect()
        db.cursor.execute(
            "SELECT id, building, room_number FROM dormitory ORDER BY building, room_number"
        )
        dormitories = db.cursor.fetchall()
        db.close()
        
        # 宿舍选择下拉框
        self.dormitory_combo = QComboBox()
        self.dormitory_combo.addItem('-- 选择宿舍 --', None)
        for dorm in dormitories:
            self.dormitory_combo.addItem(f"{dorm[1]}-{dorm[2]}", dorm[0])
        self.param_layout.addRow('宿舍:', self.dormitory_combo)
        
        self.param_group.setLayout(self.param_layout)
        self.param_group.setVisible(False)
        layout.addWidget(self.param_group)
        
        # 查询按钮
        self.query_btn = QPushButton('查询')
        self.query_btn.clicked.connect(self.perform_query)
        layout.addWidget(self.query_btn)
        
        # 结果表格
        self.result_table = QTableWidget()
        self.result_table.setColumnCount(7)
        self.result_table.setHorizontalHeaderLabels(['分配ID', '学生学号', '学生姓名', '宿舍楼号', '宿舍房间号', '状态', '分配日期'])
        self.result_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        layout.addWidget(self.result_table)
        
        # 连接信号
        self.query_button_group.buttonClicked.connect(self.on_query_option_changed)
        
        self.setLayout(layout)
    
    def on_query_option_changed(self, button):
        # 隐藏所有输入框
        for i in range(self.param_layout.rowCount()):
            self.param_layout.itemAt(i * 2 + 1).widget().setVisible(False)
            self.param_layout.itemAt(i * 2).widget().setVisible(False)
        
        # 根据选择的查询方式显示相应的输入框
        if button == self.query_by_student_btn:
            self.param_group.setVisible(True)
            self.param_layout.itemAt(0).widget().setVisible(True)  # 学号标签
            self.param_layout.itemAt(1).widget().setVisible(True)  # 学号输入框
        elif button == self.query_by_dormitory_btn:
            self.param_group.setVisible(True)
            self.param_layout.itemAt(2).widget().setVisible(True)  # 宿舍标签
            self.param_layout.itemAt(3).widget().setVisible(True)  # 宿舍下拉框
        else:
            self.param_group.setVisible(False)
    
    def perform_query(self):
        selected_option = self.query_button_group.checkedId()
        
        if selected_option == 1:  # 查询所有分配情况
            allocations = self.query.query_all_allocations()
            self.display_results(allocations)
        
        elif selected_option == 2:  # 根据学生查询分配情况
            student_id = self.student_id_input.text().strip()
            
            if not student_id:
                QMessageBox.warning(self, '警告', '学号不能为空')
                return
            
            allocations = self.query.query_allocation_by_student(student_id)
            
            if allocations:
                self.display_results(allocations)
            else:
                self.result_table.setRowCount(0)
                QMessageBox.information(self, '提示', f'没有找到学号为 {student_id} 的学生分配信息')
        
        elif selected_option == 3:  # 根据宿舍查询分配情况
            dormitory_id = self.dormitory_combo.currentData()
            
            if not dormitory_id:
                QMessageBox.warning(self, '警告', '请选择宿舍')
                return
            
            try:
                allocations = self.query.query_allocation_by_dormitory(dormitory_id)
                
                if allocations:
                    self.display_results(allocations)
                else:
                    self.result_table.setRowCount(0)
                    dorm_text = self.dormitory_combo.currentText()
                    QMessageBox.information(self, '提示', f'没有找到宿舍 {dorm_text} 的分配信息')
            except Exception as e:
                QMessageBox.warning(self, '警告', f'查询出错: {str(e)}')
    
    def display_results(self, allocations):
        if not allocations:
            self.result_table.setRowCount(0)
            QMessageBox.information(self, '提示', '没有找到任何分配信息')
            return
        
        self.result_table.setRowCount(len(allocations))
        
        for row, allocation in enumerate(allocations):
            # 检查是否是query_all_allocations返回的数据结构
            if len(allocation) > 5 and isinstance(allocation[5], str) and allocation[5] in ['active', 'inactive', 'checkout']:
                # 分配ID
                self.result_table.setItem(row, 0, QTableWidgetItem(str(allocation[0])))
                # 学生学号
                self.result_table.setItem(row, 1, QTableWidgetItem(str(allocation[1])))
                # 学生姓名
                self.result_table.setItem(row, 2, QTableWidgetItem(str(allocation[2])))
                # 宿舍楼号
                self.result_table.setItem(row, 3, QTableWidgetItem(str(allocation[3])))
                # 宿舍房间号
                self.result_table.setItem(row, 4, QTableWidgetItem(str(allocation[4])))
                # 状态
                status_text = "当前宿舍" if allocation[5] == 'active' else "原宿舍" if allocation[5] == 'inactive' else "已退宿"
                status_item = QTableWidgetItem(status_text)
                if allocation[5] == 'active':
                    status_item.setBackground(Qt.green)
                elif allocation[5] == 'inactive':
                    status_item.setBackground(Qt.yellow)
                self.result_table.setItem(row, 5, status_item)
                # 分配日期
                self.result_table.setItem(row, 6, QTableWidgetItem(str(allocation[6])))
            elif len(allocation) > 6:  # query_all_allocations返回的数据结构
                # 分配ID
                self.result_table.setItem(row, 0, QTableWidgetItem(str(allocation[0])))
                # 学生学号
                self.result_table.setItem(row, 1, QTableWidgetItem(str(allocation[3])))
                # 学生姓名
                self.result_table.setItem(row, 2, QTableWidgetItem(str(allocation[4])))
                # 宿舍楼号
                self.result_table.setItem(row, 3, QTableWidgetItem(str(allocation[5])))
                # 宿舍房间号
                self.result_table.setItem(row, 4, QTableWidgetItem(str(allocation[6])))
                # 状态
                status_text = "当前宿舍" if allocation[2] == 'active' else "原宿舍" if allocation[2] == 'inactive' else "已退宿"
                status_item = QTableWidgetItem(status_text)
                if allocation[2] == 'active':
                    status_item.setBackground(Qt.green)
                elif allocation[2] == 'inactive':
                    status_item.setBackground(Qt.yellow)
                self.result_table.setItem(row, 5, status_item)
                # 分配日期
                self.result_table.setItem(row, 6, QTableWidgetItem(str(allocation[1])))
            else:  # 其他情况
                # 分配ID
                self.result_table.setItem(row, 0, QTableWidgetItem(str(allocation[0])))
                # 学生学号
                self.result_table.setItem(row, 1, QTableWidgetItem(str(allocation[1])))
                # 学生姓名
                self.result_table.setItem(row, 2, QTableWidgetItem(str(allocation[2])))
                # 宿舍楼号
                self.result_table.setItem(row, 3, QTableWidgetItem(str(allocation[3])))
                # 宿舍房间号
                self.result_table.setItem(row, 4, QTableWidgetItem(str(allocation[4])))
                # 状态和日期列留空
                self.result_table.setItem(row, 5, QTableWidgetItem(""))
                self.result_table.setItem(row, 6, QTableWidgetItem(""))

# 宿舍分配管理主窗口
class DormitoryAllocationWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('宿舍分配管理')
        self.setGeometry(100, 100, 800, 600)
        self.initUI()
        
    def initUI(self):
        # 创建中央窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        
        # 创建选项卡窗口
        tab_widget = QTabWidget()
        
        # 创建宿舍分配管理的各个功能选项卡
        self.allocate_tab = QWidget()
        self.adjust_tab = QWidget()
        self.checkout_tab = QWidget()
        self.query_tab = QueryAllocationWidget()
        
        # 设置分配宿舍选项卡
        allocate_layout = QVBoxLayout()
        allocate_btn = QPushButton('分配宿舍')
        allocate_btn.setMinimumHeight(50)
        allocate_btn.setFont(QFont('Arial', 12))
        allocate_btn.clicked.connect(self.open_allocate_dialog)
        allocate_layout.addWidget(allocate_btn)
        
        batch_allocate_btn = QPushButton('批量分配宿舍')
        batch_allocate_btn.setMinimumHeight(50)
        batch_allocate_btn.setFont(QFont('Arial', 12))
        batch_allocate_btn.clicked.connect(self.open_batch_allocate_dialog)
        allocate_layout.addWidget(batch_allocate_btn)
        
        allocate_layout.addStretch()
        self.allocate_tab.setLayout(allocate_layout)
        
        # 设置调整宿舍选项卡
        adjust_layout = QVBoxLayout()
        adjust_btn = QPushButton('调整宿舍')
        adjust_btn.setMinimumHeight(50)
        adjust_btn.setFont(QFont('Arial', 12))
        adjust_btn.clicked.connect(self.open_adjust_dialog)
        adjust_layout.addWidget(adjust_btn)
        adjust_layout.addStretch()
        self.adjust_tab.setLayout(adjust_layout)
        
        # 设置退宿处理选项卡
        checkout_layout = QVBoxLayout()
        checkout_btn = QPushButton('退宿处理')
        checkout_btn.setMinimumHeight(50)
        checkout_btn.setFont(QFont('Arial', 12))
        checkout_btn.clicked.connect(self.open_checkout_dialog)
        checkout_layout.addWidget(checkout_btn)
        
        batch_checkout_btn = QPushButton('批量退宿处理')
        batch_checkout_btn.setMinimumHeight(50)
        batch_checkout_btn.setFont(QFont('Arial', 12))
        batch_checkout_btn.clicked.connect(self.open_batch_checkout_dialog)
        checkout_layout.addWidget(batch_checkout_btn)
        
        checkout_layout.addStretch()
        self.checkout_tab.setLayout(checkout_layout)
        
        # 添加选项卡
        tab_widget.addTab(self.allocate_tab, '分配宿舍')
        tab_widget.addTab(self.adjust_tab, '调整宿舍')
        tab_widget.addTab(self.checkout_tab, '退宿处理')
        tab_widget.addTab(self.query_tab, '查询分配情况')
        
        main_layout.addWidget(tab_widget)
        
        # 返回按钮
        back_btn = QPushButton('返回主菜单')
        back_btn.clicked.connect(self.close)
        main_layout.addWidget(back_btn)
    
    def open_allocate_dialog(self):
        dialog = AllocateDormitoryDialog(self)
        result = dialog.exec_()
        
        if result == QDialog.Accepted:
            # 如果分配成功，刷新查询选项卡的数据
            self.query_tab.perform_query()
    
    def open_batch_allocate_dialog(self):
        dialog = BatchAllocateDormitoryDialog(self)
        result = dialog.exec_()
        
        if result == QDialog.Accepted:
            # 如果批量分配成功，刷新查询选项卡的数据
            self.query_tab.perform_query()
    
    def open_adjust_dialog(self):
        dialog = AdjustDormitoryDialog(self)
        result = dialog.exec_()
        
        if result == QDialog.Accepted:
            # 如果调整成功，刷新查询选项卡的数据
            self.query_tab.perform_query()
    
    def open_checkout_dialog(self):
        dialog = CheckoutDormitoryDialog(self)
        result = dialog.exec_()
        
        if result == QDialog.Accepted:
            # 如果退宿成功，刷新查询选项卡的数据
            self.query_tab.perform_query()
    
    def open_batch_checkout_dialog(self):
        dialog = BatchCheckoutDormitoryDialog(self)
        result = dialog.exec_()
        
        if result == QDialog.Accepted:
            # 如果批量退宿成功，刷新查询选项卡的数据
            self.query_tab.perform_query()

# 如果直接运行此文件，提供测试界面
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = DormitoryAllocationWindow()
    window.show()
    sys.exit(app.exec_())